--[[
    author:Lee
    time:2021-10-13 16:29:08
]]

local require = require;
local ngx     = ngx;
local type = type;
local tostring = tostring;
local table =table;
local tonumber = tonumber;

local uu         = require("modules.commhm.utils");
local log        = require("modules.commhm.log");


local FOO = {
	_VERSION = '0.12.1',
};

-------------------------------------------------------------------------------------------------------------
-- 接口函数	: set_nginx_share
-- 说明		: 设置共享内存
-- 参数  	: cache_name	缓存名
-- 参数  	: key			键
--			: value			值
--			: exptime		过期时间
-- 返回值	: 无
-------------------------------------------------------------------------------------------------------------
FOO.set_nginx_share = function(cache_name, key, value, exptime)
	if not cache_name or type(cache_name) ~= "string" then
		log.day_list("business_common","set_nginx_share:cache_name not a string", tostring(cache_name))
		return false
	end

	if not key or not value then
		log.day_list("business_common","set_nginx_share:key or value is nil ", tostring(cache_name), tostring(key), tostring(value))
		return false
	end

--	if type(key) ~= "string" then
--		log.day_list("business_common","set_nginx_share:key not a string", tostring(cache_name), type(key))
--		return false
--	end

	if type(value) ~= "table" then
		log.day_list("business_common","set_nginx_share:value not a table", tostring(cache_name), type(value))
		return false
	end

	local cache_data = ngx.shared[cache_name]
	if not cache_data then
		log.day_list("business_common","set_nginx_share:cache_data not exist", tostring(cache_name), tostring(key))
		return false
	end

	local succ, err, forcible = cache_data:set(key, table.tostring(value), tonumber(exptime) or 0)
	if not succ then
		log.day_list("business_common","set_nginx_share:set fatal", uu.to_str({cache_name = cache_name, key = key, err = err, forcible = forcible}))
	end
	return succ
end

-------------------------------------------------------------------------------------------------------------
-- 接口函数	: get_nginx_share
-- 说明		: 获取共享内存
-- 参数  	: cache_name	缓存名
-- 参数  	: key			键
-- 返回值	: 返回key对应的共享内存数据
-------------------------------------------------------------------------------------------------------------
FOO.get_nginx_share = function(cache_name, key)
	if not cache_name or type(cache_name) ~= "string" then
		log.day_list("business_common","get_nginx_share:cache_name not a string", tostring(cache_name))
		return {}
	end

	if not key  then
		log.day_list("business_common","get_nginx_share: not key ", tostring(cache_name), type(key))
		return {}
	end

	local cache_data = ngx.shared[cache_name]
	if not cache_data then
		log.day_list("business_common","get_nginx_share:cache_data not exist", tostring(cache_name), tostring(key))
		return {}
	end

	local cache_string = cache_data:get(key)
	if not cache_string then
		return {}
	end

	if type(cache_string) ~= "string" then
		log.day_list("business_common","get_nginx_share:cache_string not a string", tostring(cache_name), type(cache_string))
		return {}
	end

	local cache_table = table.loadstring(cache_string)
	if not cache_table or type(cache_table) ~= "table" then
		log.day_list("business_common","get_nginx_share:cache_data not a table", tostring(cache_name), type(cache_data))
		return {}
	end

	return cache_table
end

-------------------------------------------------------------------------------------------------------------
-- 接口函数	: del_nginx_share
-- 说明		: 删除共享内存
-- 参数  	: cache_name	缓存名
-- 参数  	: key			键
-- 返回值	: 无
-------------------------------------------------------------------------------------------------------------
FOO.del_nginx_share = function(cache_name, key)
	if not cache_name or type(cache_name) ~= "string" then
		log.day_list("business_common","del_nginx_share:cache_name not a string", tostring(cache_name))
		return {}
	end

	if not key or type(key) ~= "string" then
		log.day_list("business_common","del_nginx_share:key not a string", type(key))
		return
	end

	local cache_data = ngx.shared[cache_name]
	if not cache_data then
		log.day_list("business_common","del_nginx_share:cache_data not exist", tostring(cache_name), tostring(key))
		return {}
	end

	cache_data:delete(key)
end

-------------------------------------------------------------------------------------------------------------
-- 接口函数	: clear_nginx_share
-- 说明		: 清除共享内存
-- 参数  	: cache_name	缓存名
-- 返回值	: 无
-------------------------------------------------------------------------------------------------------------
FOO.clear_nginx_share = function(cache_name)
	if not cache_name or type(cache_name) ~= "string" then
		log.day_list("business_common","clear_nginx_share:cache_name not a string", tostring(cache_name))
		return
	end

	local cache_data = ngx.shared[cache_name]
	if not cache_data then
		log.day_list("business_common","clear_nginx_share:cache_data not exist", tostring(cache_name))
		return
	end

	cache_data:flush_all()
end

-------------------------------------------------------------------------------------------------------------
-- 接口函数	: flush_nginx_share
-- 说明		: 清除过期共享内存
-- 参数  	: cache_name	缓存名
-- 返回值	: 无
-------------------------------------------------------------------------------------------------------------
FOO.flush_nginx_share = function(cache_name)
	if not cache_name or type(cache_name) ~= "string" then
		log.day_list("business_common","flush_nginx_share:cache_name not a string", tostring(cache_name))
		return
	end

	local cache_data = ngx.shared[cache_name]
	if not cache_data then
		log.day_list("business_common","flush_nginx_share:cache_data not exist", tostring(cache_name))
		return
	end

	cache_data:flush_expired()
end

-------------------------------------------------------------------------------------------------------------
-- 接口函数	: get_keys_share
-- 说明		: 获取共享内存所有key
-- 参数  	: cache_name	缓存名
-- 参数  	: max_count			最大数量
-- 返回值	: 无
----------------------------------------------
FOO.get_keys_share = function(cache_name, max_count)
	if not cache_name or type(cache_name) ~= "string" then
		log.day_list("business_common","get_keys_share:cache_name not a string", tostring(cache_name))
		return {}
	end

	local cache_data = ngx.shared[cache_name]
	if not cache_data then
		log.day_list("business_common","get_keys_share:cache_data not exist", tostring(cache_name))
		return {}
	end

	return cache_data:get_keys(max_count)
end


return FOO